package com.arrays.a1;

import java.util.Arrays;

/*删除有序数组中的重复项26
 */
public class ArrayDistinct26 {
    public static void main(String[] args) {
        int[] a = {0, 0, 1, 1, 1, 2, 2, 3, 3, 4};

        int k = 0;
        for (int i = 1; i < a.length; i++) {   // k=0 i=1       k=0 i=2         k=1 i=3
            if (a[k] != a[i]) {                // a[0]=a[1]     a[0]!=a[2]      a[1]!=a[3]
                k+=1;                          //               k=1             k=2
                a[k] = a[i];                   //               a[1]=a[2]=0     a[2]=a[3]=1
            }
        }
        System.out.println(Arrays.toString(a));
        System.out.println("去重后数组长度应为"+(k+1));
        int[] res = Arrays.copyOf(a, 5);
        System.out.println(Arrays.toString(res));
    }

}
